package cn.initcap.authorize;

import cn.initcap.properties.SecurityConstants;
import cn.initcap.properties.SecurityProperties;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

/**
 * @author initcap
 * @Order用于指定加载顺序，值小的优先加载
 * @date Created in 2018/5/19 PM9:23.
 */
@Component
@Order(Integer.MIN_VALUE)
public class InitcapAuthorizeConfigProvider implements AuthorizeConfigProvider {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public boolean config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {

        config.antMatchers(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_MOBILE,
                SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_OPENID,
                SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/*",
                securityProperties.getWeb().getSignInPage(),
                securityProperties.getWeb().getSignUpUrl(),
                securityProperties.getWeb().getSession().getSessionInvalidUrl()).permitAll();

        if (StringUtils.isNotBlank(securityProperties.getWeb().getSignOutUrl())) {
            config.antMatchers(securityProperties.getWeb().getSignOutUrl()).permitAll();
        }
        return false;
    }

}
